<template>
	<view class="container" :style="appThemeStyle">
		<!-- 页面头部 -->
		<view class="main-header"
			:style="{ height: platform == 'H5' ? '260rpx' : '320rpx', paddingTop: platform == 'H5' ? '0' : '80rpx' }">
			<image class="bg-image" src="/static/background/user-header2.png" mode="scaleToFill"></image>
			<!-- 用户信息 -->
			<view v-if="isLogin" class="user-info">
				<view class="user-avatar" @click="handleService('/pages/mine/personal/geren')">
					<avatar-image :url="apiUrl + userInfo.pic" :width="100" />
				</view>
				<view class="user-content" @click="handleService('/pages/mine/personal/geren')">
					<!-- 会员昵称 -->
					<view class="nick-name oneline-hide">{{ userInfo.nickname }}</view>

					<view class="mobile">账号名：{{ userInfo.account }}</view>
				</view>
				<view style="margin-left: 150rpx;" @click="handlePersonal()">
					<view>设置</view>
					<view style="margin-top: -15rpx;"><uni-icons type="gear" size="30"></uni-icons>
					</view>
				</view>
			</view>
			<!-- 未登录 -->
			<view v-else class="user-info" @tap="handleLogin">
				<view class="user-avatar">
					<avatar-image :url="avatarUrl" :width="100" />
				</view>
				<view class="user-content">
					<view class="nick-name">未登录</view>
					<view class="login-tips">点击登录账号</view>
				</view>
			</view>
		</view>


		<!-- 我的钱包 -->
		<view class="my-asset">
			<view class="asset-left flex-box dis-flex flex-x-around">
				<view class="asset-left-item" style="max-width: 200rpx;" @click="onTargetWallet">
					<view class="item-value dis-flex flex-x-center">
						<text class="oneline-hide">{{ isLogin ?  userInfo.balance : '--' }}</text>
					</view>
					<view class="item-name dis-flex flex-x-center">
						<text>账户余额</text>
					</view>
				</view>
				<view class="asset-left-item" @click="onTargetPoints">
					<view class="item-value dis-flex flex-x-center">
						<text class="oneline-hide">{{ isLogin ?  userInfo.fen : '--' }}</text>
					</view>
					<view class="item-name dis-flex flex-x-center">
						<text>积分</text>
					</view>
				</view>
				<view class="asset-left-item" @click="onTargetMyCoupon">
					<view class="item-value dis-flex flex-x-center">
						<text class="oneline-hide">{{ isLogin ?  userInfo.juan : '--' }}</text>
					</view>
					<view class="item-name dis-flex flex-x-center">
						<text>优惠券</text>
					</view>
				</view>
			</view>
			<view class="asset-right">
				<view class="asset-right-item" @click="onTargetWallet">
					<view class="item-icon dis-flex flex-x-center">
						<!-- <text class="iconfont icon-qianbao"></text> -->
						<uni-icons type="wallet" size="30"></uni-icons>
					</view>
					<view class="item-name dis-flex flex-x-center">
						<text>我的钱包</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 订单操作 -->
		<!-- <view class="order-navbar">
			<view class="order-navbar-item" v-for="(item, index) in orderNavbar" :key="index"
				@click="onTargetOrder(item)">
				<view class="item-icon">
					<text class="iconfont" :class="[`icon-${item.icon}`]"></text>
				</view>
				<view class="item-name">{{ item.name }}</view>
				<view class="item-badge" v-if="item.count && item.count > 0">
					<text v-if="item.count <= 99" class="text">{{ item.count }}</text>
					<text v-else class="text">99+</text>
				</view>
			</view>
		</view> -->
		<!-- 订单操作 -->
		<view class="order-navbar">
			<view class="order-navbar-item" @click="onTargetOrder('')">
				<view class="item-icon">
					<uni-badge class="uni-badge-left-margin" :text="3" absolute="rightTop" :offset="[-3, -3]" size="normal">
						<text class="iconfont" :class="[`icon-qpdingdan`]" style="font-size: 44rpx;"></text>
					</uni-badge>
				</view>
				<view class="item-name">全部订单</view>
			</view>
			<view class="order-navbar-item" @click="onTargetOrder(1)">
				<view class="item-icon">
					<uni-badge class="uni-badge-left-margin"  absolute="rightTop" :offset="[-3, -3]" size="normal">
						<text class="iconfont" :class="[`icon-daifukuan`]" style="font-size: 44rpx;"></text>
					</uni-badge>
				</view>
				<view class="item-name">待支付</view>
			</view>
			<view class="order-navbar-item" @click="onTargetOrder(3)">
				<view class="item-icon">
					<uni-badge class="uni-badge-left-margin" :text="1" absolute="rightTop" :offset="[-3, -3]" size="normal">
						<text class="iconfont" :class="[`icon-daifahuo`]" style="font-size: 44rpx;"></text>
					</uni-badge>
				</view>
				<view class="item-name">服务中</view>
			</view>
			<view class="order-navbar-item" @click="onTargetOrder(4)">
				<view class="item-icon">
					<uni-badge class="uni-badge-left-margin" :text="2" absolute="rightTop" :offset="[-3, -3]" size="normal">
						<text class="iconfont" :class="[`icon-daishouhuo`]" style="font-size: 44rpx;"></text>
					</uni-badge>
				</view>
				<view class="item-name">已完成</view>
			</view>
		</view>

		<!-- 我的服务 -->
		<view class="my-service">
			<view class="service-title">我的服务</view>
			<view class="service-content clearfix">
				<view class="service-item" @click="handleService('/pages/mine/address/address')">
					<view class="item-icon">
						<text class="iconfont" :class="[`icon-shouhuodizhi`]"></text>
					</view>
					<view class="item-name">地址管理</view>
				</view>
				<view class="service-item" @click="handleService('/pages/mine/kefu/kefu')">
					<view class="item-icon">
						<text class="iconfont" :class="[`icon-kefu`]"></text>
					</view>
					<view class="item-name">在线客服</view>
				</view>
				<view class="service-item" @click="onTargetOrder('')">
					<view class="item-icon">
						<text class="iconfont" :class="[`icon-order-c`]"></text>
					</view>
					<view class="item-name">订单中心</view>
				</view>
				<view class="service-item" @click="handleService(item)">
					<view class="item-icon">
						<text class="iconfont" :class="[`icon-bangzhu`]"></text>
					</view>
					<view class="item-name">我的帮助</view>
				</view>
				<view class="service-item" @click="handleService('/pages/mine/patient/select-patient')">
					<view class="item-icon">
						<text class="iconfont" :class="[`icon-tuandui`]"></text>
					</view>
					<view class="item-name">就诊人管理</view>
				</view>
				<!-- <view class="service-item" @click="handleService('/pages/mine/address/address')">
					<view class="item-icon">
						<text class="iconfont" :class="[`icon-profile`]"></text>
					</view>
					<view class="item-name">实名认证</view>
				</view> -->
				<view class="service-item" @click="handleService('/pages/mine/address/address')">
					<view class="item-icon">
						<text class="iconfont" :class="[`icon-kanjia`]"></text>
					</view>
					<view class="item-name">退款/售后</view>
				</view>
				<view class="service-item" @click="handleService('/pages/mine/relation/relation')">
					<view class="item-icon">
						<text class="iconfont" :class="[`icon-profile`]"></text>
					</view>
					<view class="item-name">亲友</view>
				</view>

			</view>
		</view>

		<!-- 退出登录 -->
		<!-- <view class="my-logout" v-if="isLogin"> <button @click="handleLogout()" class="logout-btn">退出登录</button></view> -->
	</view>
</template>

<script>
	import store from '@/store'
	import {
		inArray
	} from '@/utils/util'
	import AvatarImage from '@/components/avatar-image'
	import CustomerBtn from '@/components/customer-btn'
	import {
		setCartTabBadge
	} from '@/core/app'
	import SettingKeyEnum from '@/common/enum/setting/Key'
	import StoreModel from '@/common/model/Store'
	import SettingModel from '@/common/model/Setting'
	import * as UserApi from '@/api/user'
	import * as OrderApi from '@/api/order'
	import {
		checkLogin,
		filterModules
	} from '@/core/app'
	import {
		apiUrl
	} from '@/config/config.js';

	// 订单操作
	const orderNavbar = [{
			id: 'all',
			name: '全部订单',
			icon: 'qpdingdan'
		},
		{
			id: 'payment',
			name: '待支付',
			icon: 'daifukuan',
			count: 0
		},
		{
			id: 'delivery',
			name: '进行中',
			icon: 'daifahuo',
			count: 0
		},
		{
			id: 'received',
			name: '已完成',
			icon: 'daishouhuo',
			count: 0
		},
	]

	/**
	 * 我的服务
	 * id: 标识; name: 标题名称; icon: 图标; type 类型(link和button); url: 跳转的链接
	 */
	const service = [{
			id: 'address',
			name: '地址管理',
			icon: 'shouhuodizhi',
			type: 'link',
			url: 'pages/address/index'
		},
		{
			id: 'coupon',
			name: '领券中心',
			icon: 'lingquan',
			type: 'link',
			url: 'pages/coupon/index',
			moduleKey: 'market-coupon'
		},
		{
			id: 'myCoupon',
			name: '优惠券',
			icon: 'youhuiquan',
			type: 'link',
			url: 'pages/my-coupon/index',
			moduleKey: 'market-coupon'
		},
		{
			id: 'refund',
			name: '退换/售后',
			icon: 'shouhou',
			type: 'link',
			url: 'pages/refund/index',
			count: 0
		},
		{
			id: 'contact',
			name: '在线客服',
			icon: 'kefu',
			type: 'contact'
		},
		{
			id: 'points',
			name: '我的积分',
			icon: 'jifen',
			type: 'link',
			url: 'pages/points/log',
			moduleKey: 'market-points'
		},
		{
			id: 'orderCenter',
			name: '订单中心',
			icon: 'order-c',
			type: 'link',
			url: 'pages/order/center'
		},
		{
			id: 'help',
			name: '我的帮助',
			icon: 'bangzhu',
			type: 'link',
			url: 'pages/help/index',
			moduleKey: 'content-help'
		},
	]

	export default {
		components: {
			AvatarImage,
			CustomerBtn
		},
		data() {
			return {
				inArray,
				// 枚举类
				SettingKeyEnum,
				// 正在加载
				isLoading: true,
				// 首次加载
				isFirstload: true,
				// 是否已登录
				isLogin: false,
				// 系统设置
				setting: {},
				// 当前用户信息
				userInfo: {},
				// 账户资产
				assets: {
					balance: '--',
					points: '--',
					coupon: '--'
				},
				// 我的服务
				service,
				// 订单操作
				orderNavbar,
				// 当前用户待处理的订单数量
				todoCounts: {
					payment: 0,
					deliver: 0,
					received: 0
				},
				avatarUrl: "/static/images/mine/avater.png",
				apiUrl: apiUrl,
			}
		},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onLoad(options) {},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow(options) {
			this.onRefreshPage()
		},

		methods: {

			// 刷新页面
			onRefreshPage() {
				// 判断是否已登录
				this.checkLogin1()
				// 获取页面数据
				this.getPageData()
			},

			checkLogin1() {
				try {
					const value = uni.getStorageSync('islogin_key');
					if (value) {
						this.isLogin = value;
					}
				} catch (e) {
					// error
				}

			},

			// 获取页面数据
			getPageData(callback) {
				// console.log(uni.getStorageSync('account_key'))

				if (this.isLogin == true) {
					try {
						uni.request({
							url: this.apiUrl + '/api/user/userInfo',
							data: {
								account: uni.getStorageSync('account_key')
							},
							method: 'GET',
							success: (res) => {
								if (res.data.code == '200') {
									this.userInfo = res.data.data
								} else {

								}
							}
						})

					} catch (e) {
						// error
					}
				}

			},

			// 跳转到登录页
			handleLogin() {
				!this.isLogin
				uni.navigateTo({
					url: '/pages/login/login'
				});
			},

			// 跳转到绑定手机号页面
			handleBindMobile() {
				this.$navTo('pages/user/bind/index')
			},

			// 跳转到修改个人信息页
			handlePersonal() {
				uni.navigateTo({
					url: '/pages/mine/personal/personal'
				});
			},

			// 退出登录
			handleLogout() {
				const app = this
				uni.showModal({
					title: '友情提示',
					content: '您确定要退出登录吗?',
					success(res) {
						if (res.confirm) {
							try {
								uni.removeStorageSync('islogin_key');
							} catch (e) {
								// error
							}
							this.isLogin = false
							uni.reLaunch({
								url: '/pages/mine/index' // 将'/pages/currentPage/currentPage'替换为当前页面的路径
							});
						}
					}
				})
			},

			// 跳转到钱包页面
			onTargetWallet() {
				uni.navigateTo({
					url: '/pages/mine/wallet/index'
				});
			},

			// 跳转到订单页
			onTargetOrder(item) {
				// uni.navigateTo({
				// 	url: '/pages/order/index'
				// });
				uni.navigateTo({
					url: `/pages/mine/order/order?filt=${item}`
				});

			},

			// 跳转到我的积分页面
			onTargetPoints() {
				this.$navTo('pages/points/log')
			},

			// 跳转到我的优惠券页
			onTargetMyCoupon() {
				this.$navTo('pages/my-coupon/index')
			},

			// 跳转到服务页面
			handleService(route) {
				// console.log(route)
				uni.navigateTo({
					url: route
				});
			},

		},

		/**
		 * 下拉刷新
		 */
		onPullDownRefresh() {
			// 获取首页数据
			this.getPageData(() => {
				uni.stopPullDownRefresh()
			})
		},


	}
</script>

<style lang="scss" scoped>
	.container {
		padding-bottom: 60rpx;
	}

	// 页面头部
	.main-header {
		// background-color: #FBF7EF;
		position: relative;
		width: 100%;
		height: 280rpx;
		background-size: 100% 100%;
		overflow: hidden;
		display: flex;
		align-items: center;
		padding-left: 30rpx;

		.bg-image {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			z-index: 0;
		}

		.user-info {
			display: flex;
			height: 100rpx;
			z-index: 1;

			.user-content {
				display: flex;
				flex-direction: column;
				justify-content: center;
				margin-left: 30rpx;
				color: #c59a46;

				.nick-name {
					font-size: 34rpx;
					font-weight: bold;
					max-width: 300rpx;
				}

				.mobile {
					margin-top: 15rpx;
					font-size: 28rpx;
				}

				.user-grade {
					align-self: baseline;
					display: flex;
					align-items: center;
					background: #3c3c3c;
					margin-top: 12rpx;
					border-radius: 10rpx;
					padding: 4rpx 12rpx;

					.user-grade_icon .image {
						display: block;
						width: 32rpx;
						height: 32rpx;
					}

					.user-grade_name {
						margin-left: 5rpx;
						font-size: 26rpx;
						color: #EEE0C3;
					}

				}

				.login-tips {
					margin-top: 12rpx;
					font-size: 30rpx;
				}

			}
		}
	}

	// 角标组件
	.item-badge {
		position: absolute;
		top: 0;
		right: 55rpx;
		// background: $main-bg;
		background: #fa2209;
		color: #fff;
		border-radius: 100%;
		min-width: 38rpx;
		height: 38rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 1rpx;
		font-size: 24rpx;
	}

	// 我的钱包
	.my-asset {
		display: flex;
		background: #fff;
		padding: 40rpx 0;

		.asset-left {
			display: flex;
			flex: 1;
		}

		.asset-left-item {
			flex: 1;
			text-align: center;
			color: #666;
			padding: 0 16rpx;

			.item-value {
				font-size: 34rpx;
				color: #fa2209;
			}

			.item-name {
				margin-top: 14rpx;
				font-size: 28rpx;
			}

		}

		.asset-right {
			width: 200rpx;
			border-left: 1rpx solid #eee;
		}

		.asset-right-item {
			text-align: center;
			color: #545454;

			.item-icon {
				font-size: 44rpx;
			}

			.item-name {
				margin-top: 14rpx;
				font-size: 28rpx;
			}

		}
	}

	// 订单操作
	.order-navbar {
		display: flex;
		margin: 20rpx auto 20rpx auto;
		padding: 20rpx 0 26rpx 0;
		width: 94%;
		box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
		font-size: 30rpx;
		border-radius: 5rpx;
		background: #fff;

		&-item {
			position: relative;
			width: 25%;

			.item-icon {
				text-align: center;
				margin: 0 auto;
				padding: 10rpx 0;
				color: #545454;
				font-size: 44rpx;
			}

			.item-name {
				font-size: 28rpx;
				color: #545454;
				text-align: center;
				margin-right: 10rpx;
			}

		}
	}

	// 我的服务
	.my-service {
		margin: 22rpx auto 22rpx auto;
		padding: 22rpx 0;
		width: 94%;
		box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
		border-radius: 5rpx;
		background: #fff;

		.service-title {
			padding-left: 24rpx;
			margin-bottom: 20rpx;
			font-size: 30rpx;
		}

		.service-content {

			margin-bottom: -20rpx;

			.service-item {
				position: relative;
				width: 25%;
				float: left;
				background: #fff;
				// margin-bottom: 30rpx;

				.item-icon {
					text-align: center;
					margin: 0 auto;
					padding: 14rpx 0;
					// color: #fa2209;
					color: #1c986b;
					font-size: 44rpx;
				}

				.item-name {
					font-size: 28rpx;
					color: #545454;
					text-align: center;
				}

			}
		}
	}



	// 退出登录
	.my-logout {}

	.my-logout .logout-btn {
		width: 94%;
		font-size: 28rpx;
		color: #fff;
		border-radius: 20rpx;
		border: 1px solid #dcdcdc;
		padding: 16rpx 0;
		text-align: center;
		margin-top: 20rpx;
		/* 调整按钮与上方元素的间距 */
		background-color: #fa2209;
		// height: 80rpx;
		border-radius: 80rpx;

	}



	// 绑定手机号 样式1
	.my-mobile {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 16rpx 40rpx;
		background: #FCEBD1;

		.info {
			color: #cd8c0c;
			font-size: 28rpx;
		}

		.btn-bind {
			padding: 8rpx 24rpx;
			background-color: #EAB766;
			color: #fff;
			border-radius: 30rpx;
			font-size: 26rpx;
			text-align: center;
		}
	}

	// 绑定手机号 样式2
	.my-mobile2 {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin: 20rpx auto 20rpx auto;
		padding: 12rpx 40rpx;
		width: 94%;
		box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
		font-size: 30rpx;
		border-radius: 5rpx;
		background: #fff;

		.info {
			// color: #cd8c0c;
			font-size: 26rpx;
		}

		.btn-bind {
			padding: 8rpx 24rpx;
			background-color: #EAB766;
			color: #fff;
			border-radius: 30rpx;
			font-size: 26rpx;
			text-align: center;
		}
	}
</style>